package com.tbit.uqbike.client.remote.nameauth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.client.util.HttpUtils;
import com.tbit.uqbike.client.util.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 阿里云实名认证
 * @Date: 创建于 15:19 2021/4/15
 */
@Slf4j
public class AliyunNameAuthUtil {
    /**
     * 域名
     */
    public static String host = "https://lfeid.market.alicloudapi.com";
    /**
     * 接口地址
     */
    public static String path = "/idcheck/life";

    public static Boolean nameAuth(String appcode, String idNO, String name) {

        String method = "GET";
        //String appcode = "你自己的AppCode";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        //Map<String, String> bodys = new HashMap<String, String>();
        querys.put("cardNo", idNO);
        querys.put("realName", name);
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            // HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys,bodys);
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            String tmp = EntityUtils.toString(response.getEntity());
            LogUtil.info("nameauth=" + tmp);
            if (tmp != "" && tmp != null) {
                JSONObject jsonObject = JSON.parseObject(tmp);
                if (jsonObject.getInteger("error_code") == 0) {
                    Boolean isok = jsonObject.getJSONObject("result").getBoolean("isok");
                    return isok;
                }
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

    /**
     * 实名认证3要素
     *
     * @param host
     * @param path
     * @param appcode
     * @param idCard
     * @param phone
     * @param name
     * @return
     */
    public static boolean nameAuth3(String host, String path, String appcode, String idCard, String phone, String name) {
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("idCard", idCard);
        bodys.put("mobile", phone);
        bodys.put("realName", name);
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //System.out.println(response.toString());
            String tmp = EntityUtils.toString(response.getEntity());
            log.info("三要素认证返回结果=" + tmp);
            if (tmp != "" && tmp != null) {
                JSONObject jsonObject = JSON.parseObject(tmp);
                if (jsonObject.getInteger("error_code") == 0) {
                    Integer result = jsonObject.getJSONObject("result").getInteger("VerificationResult");
                    if (result == 1) {
                        return true;
                    }
                }
            }
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            log.error("3要素认证异常", e);
        }
        return false;
    }


    public static void main(String[] args) {
        String key = "host=https://zdx.market.alicloudapi.com&path=/efficient/cellphone/post&appcode=5d086f81e4924fcdbf823659c8db2ccc";
        // String key =config.getParamKV();
        String[] strings = key.split("&");
        String host = strings[0].substring(strings[0].indexOf("=") + 1);
        String path = strings[1].substring(strings[1].indexOf("=") + 1);
        String appcode = strings[2].substring(strings[2].indexOf("=") + 1);
    /*String host = "https://zdx.market.alicloudapi.com";
    String path = "/efficient/cellphone/post";
    String appcode = "5d086f81e4924fcdbf823659c8db2ccc";*/
        // System.out.println(nameAuth3(host,path,appcode,"142202199604154877","17735767747","王德立"));
        System.out.println(nameAuth("4de1a13fd1c3409b9b62f515c16645a5", "440921199008022110", "陈梓茂"));
    }
}
